<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Luckydraw extends My_Controller {
    public function __construct()
    {

        parent::__construct();
        $this->load->model('luckydraw_model', 'luckydraw');
        $this->load->model('user_model', 'user');
        $this->load->model('address_model', 'address');
    }
    /**
     *------------------------------------------------------------------------------------------------------------------
     * @title：根据分类id获取产品列表
     * @action：/Item/index.json
     * @method：GET
     * @param：cat_id             类型：INT，必须：YES ，描述：分类id
     * @author:：KevinBai
     *------------------------------------------------------------------------------------------------------------------
     */
    public function index()
    {
        $limit = 8;
        $this->isArrays = true;
        $params['per_page'] = $this->queryData['per_page'] ? $this->queryData['per_page'] : 1;
        $validate = new Validate([
            'per_page' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $fields = " id, title, price, summary,provinceid,cityid,area ,gettype,isempty,chance";
        $luckydrawList = $this->luckydraw->get_luckydraw_list($fields, $params['per_page'],$limit);
        foreach ($luckydrawList as $key => $value) {
            $luckydrawList[$key]['provincename'] = "";
            $luckydrawList[$key]['cityname'] = "";
            if($value['gettype'] == 1)
            {
                $luckydrawList[$key]['provincename'] = $this->address->getRegionById($value['provinceid'],'name')['name'];

                $luckydrawList[$key]['cityname'] =  $this->address->getRegionById($value['cityid'],'name')['name'];
            }

            $luckydrawList[$key]['summary'] = cutstr(strip_tags($luckydrawList[$key]['summary']),30);
            $imgurl = $this->luckydraw->get_default_cover($value['id']);
            $luckydrawList[$key]['imgurl'] = $imgurl ? OSSHOST.$imgurl : "";
        }
        
        $this->createApi($luckydrawList);
    }


  
    public function detail(){
        $params['id'] = $this->queryData['id'];
        $validate = new Validate([
            'id' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }

        $luckydrawInfo = $this->luckydraw->get_luckydraw_by_id($params['id'], " * ");
        unset($luckydrawInfo['isDel']);
        unset($luckydrawInfo['onlinestatus']);
        unset($luckydrawInfo['addtime']);
        //获取产品封面轮播图
        $luckydrawCover = $this->luckydraw->get_img_list_byid($params['id'], "imgurl");
        if($luckydrawCover){
            foreach ($luckydrawCover as $key => $value) {
                $luckydrawCover[$key]['imgurl'] = $value['imgurl'] ? OSSHOST.$value['imgurl'] : "";
            }
        }
        if(!empty($luckydrawInfo)){
            $luckydrawInfo['cover'] = $luckydrawCover;
        }
        $userInfo = $this->userDetail;
        $luckydrawInfo['myPoints'] = 0;
        if($userInfo){
            $luckydrawInfo['myPoints'] = $userInfo['points'];
        }
        
        $this->createApi($luckydrawInfo);
    }
   


    public function orderList(){
        $per_page = $this->queryData['per_page']?$this->queryData['per_page']:1;
        $limit  = $this->config->item('limit');
        $this->isArrays = true;
        $orderList = array();
        $userInfo = $this->userDetail;
        $orderList = $this->luckydraw->getOrderList($userInfo['AccountID'],$per_page, $limit, " id,AccountID,goodsID,goodsName,status,receiver,mobile,address,createtime,goodsimgurl,gettype,expressName,expressNo");      
	    foreach ($orderList as $key => $value) {
            $orderList[$key]['createtime'] = date("Y-m-d H:i:s", $value['createtime']);
			$orderList[$key]['goodsimgurl'] = $value['goodsimgurl']?OSSHOST.$value['goodsimgurl']:"";
        }
        $this->createApi($orderList);
    }

    public function delete(){
        $params['id'] = $this->queryData['id'];
        $validate = new Validate([
            'id' => 'require|number',
        ]);

        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $userInfo = $this->userDetail;
        $params['AccountID'] = $userInfo['AccountID'];
        $res = $this->luckydraw->deleteOrder($params);
        if(!$res){
            $this->errorCode = 50006;
        }
        $this->createApi();
    }


    
     /**
     *------------------------------------------------------------------------------------------------------------------
     * @title：提交订单
     * @action：/luckydraw/order
     * @method：POST
     * @author:：柏凯
     *------------------------------------------------------------------------------------------------------------------
     */
    public function submit(){
         
		$params['address'] = $this->queryData['address'];
        $params['mobile'] = $this->queryData['mobile'];
        $params['receiver'] = $this->queryData['receiver'];
        $params['id'] = $this->queryData['id'];
		$params['gettype'] = $this->queryData['gettype'];
		if($params['gettype'] == 2)
		{
			$validate = new Validate([
				'address' => 'require|string',
				'mobile' => 'require|string',
				'receiver' => 'require|string',
				'id' => 'require|number',
			]);
		}else{
			$validate = new Validate([
            'address' => 'require|string',
            'id' => 'require|number',
        ]);
		}
        
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
		
        $userInfo = $this->userDetail;
         $luckydrayYearMonth = $this->luckydraw->getLuckydrayYearMonth();
         $luckydrawUser = [];
         if($luckydrayYearMonth)
         {
            foreach ($luckydrayYearMonth as $key => $value) 
            {
                $luckydrawUser[] = $value['AccountID'];
            }

            if(!in_array($userInfo['AccountID'],$luckydrawUser))
            {
                $this->errorCode = 90011;
                $this->createApi();exit;
            }
         }
        $luckydrawInfo = $this->luckydraw->get_luckydraw_by_id($params['id']," * ");
        if(empty($luckydrawInfo)){
            $this->errorCode = 90003;
            $this->createApi();exit;
        }
        if($luckydrawInfo['stock']<=0){
            $this->errorCode = 90008;
            $this->createApi();exit;
        }
        //获取默认图片
        $imgurl = $this->luckydraw->get_default_cover($luckydrawInfo['id']);
        $luckydrawDate['goodsID'] = $luckydrawInfo['id'];
        $luckydrawDate['goodsName'] = $luckydrawInfo['title'];
        $luckydrawDate['goodsimgurl'] = $imgurl;
        $luckydrawDate['AccountID'] = $userInfo['AccountID'];
        $luckydrawDate['createtime'] = time();
        $luckydrawDate['address'] = $params['address'];
        $luckydrawDate['receiver'] = $params['receiver'];
        $luckydrawDate['mobile'] = $params['mobile'];
        $luckydrawDate['gettype'] = $luckydrawInfo['gettype'];
        $res = $this->luckydraw->submit($luckydrawDate);
		if(empty($res))
        {
            $this->errorCode = 50006;
            $this->createApi();exit;    
        }
        $this->luckydraw->editLuckydrawStock($luckydrawInfo['id']);
        $this->createApi();

    }


}

/* End of file category.php */
/* Location: ./application/controllers/luckydog/category.php */
